﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProtoType
{
    public class GumballMachine
    {
        static int SOLD_OUT = 0;
        static int NO_QUARTER = 1;
        static int HAS_QUARTER = 2;
        static int SOLD = 3;

        int state = SOLD_OUT;
        int cout = 0;

        public GumballMachine(int cout)
        {
            this.cout = cout;
            if(cout > 0)
            {
                state = NO_QUARTER;
            }
        }

        public void InsertQuarter()
        {
            if(state == HAS_QUARTER)
            {
                Console.WriteLine("you can't insert another quarter");
            }
            else if(state == NO_QUARTER)
            {
                state = HAS_QUARTER;
                Console.WriteLine("you inserted a quarter");
            }
            else if(state == SOLD_OUT)
            {
                Console.WriteLine("you can't insert a quarter, the machine is sold out");
            }
            else if(state == SOLD)
            {
                Console.WriteLine("please wait, we're already giving you a gumball");
            }
        }

        public void EjectQuarter()
        {
            if(state == HAS_QUARTER)
            {
                Console.WriteLine("quarter returned");
                state = NO_QUARTER;
            }
            else if(state == NO_QUARTER)
            {
                Console.WriteLine("you haven't inserted a quarter");
            }
            else if(state == SOLD)
            {
                Console.WriteLine("sorry, you already turned the crank");
            }
            else if(state == SOLD_OUT)
            {
                Console.WriteLine("you can't eject, you haven't inserted a quarter yet");
            }
        }

        public void TurnCrank()
        {
            if(state == SOLD)
            {
                Console.WriteLine("turning twice doesn't get you another gumball!");
            }
            else if(state == NO_QUARTER)
            {
                Console.WriteLine("you turned but there's no quarter");
            }
            else if(state == SOLD_OUT)
            {
                Console.WriteLine("you turned, but there's are no gumballs");
            }
            else if (state == HAS_QUARTER)
            {
                Console.WriteLine("you turned...");
                state = SOLD;
                
            }
        }

        public void Dispense()
        {
            if(state == SOLD)
            {
                Console.WriteLine("a gumball comes rolling out the slot");
                cout--;
                if(0 == cout)
                {
                    Console.WriteLine("oops, out of gumballs");
                    state = SOLD_OUT;
                }
                else
                {
                    state = NO_QUARTER;
                }
            }
            else if(state == NO_QUARTER)
            {
                Console.WriteLine("you need to pay first");
            }
            else if(state == SOLD_OUT)
            {
                Console.WriteLine("no gumball dispensed");
            }
            else if(state == HAS_QUARTER)
            {
                Console.WriteLine("no gumball dispensed");
            }
        }
    }
}
